package cn.tl.org.controller;

import cn.tl.org.domain.Detail;
import cn.tl.org.domain.Type;
import cn.tl.org.query.DetailQuery;
import cn.tl.org.query.TypeQuery;
import cn.tl.org.service.IDetailService;
import cn.tl.org.service.ITypeService;
import cn.tl.org.utils.AjaxResult;
import cn.tl.org.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
@Api(tags = "数字字典明细")
//@CrossOrigin
public class TypeController {
    @Autowired
    private ITypeService typeService;
    @PutMapping
    @ApiOperation(value = "新增或修改接口")
    public AjaxResult saveOrUpt(@RequestBody Type type){
        try {
            if (type.getId() == null){
                typeService.save(type);
            }else {
                typeService.update(type);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            typeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Type findById(@PathVariable("id") Long id){
        return typeService.findById(id);
    }
    @GetMapping
    public List<Type> findAll(){
        return typeService.findAll();
    }

    @PostMapping
    public QueryResult<Type> queryPage(@RequestBody TypeQuery query){
        return typeService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            typeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }


    }







